package com.ruoyi.project.app.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.app.intercepter.FilterAnnotation;
import com.ruoyi.project.app.request.OrderRepayRequest;
import com.ruoyi.project.app.response.OrderRepayPlanResponse;
import com.ruoyi.project.app.util.XResult;
import com.ruoyi.project.beautyflower.order.domain.Order;
import com.ruoyi.project.beautyflower.order.service.OrderServiceImpl;
import com.ruoyi.project.beautyflower.paymentHistory.service.PaymentHistoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 订单模块
 */
@RestController
@RequestMapping("/app/order")
@Api(tags = "订单模块")
public class AppOrderController extends BaseController {
    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private PaymentHistoryServiceImpl paymentHistoryService;//还款记录


    /**
     * 根据用户查询订单列表
     * @param request
     * @return
     */
    @PostMapping("selectOrderList")
    @ApiOperation(notes = "selectOrderList", value = "根据用户查询订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "当前显示数据条数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "status", value = "订单状态（0：放款中，1：待还款2：正在还款，3:已还清4：全部）", required = false, paramType = "query", dataType = "int"),
    })
    public Object selectOrderList(@ApiIgnore HttpServletRequest request) {
        Order order = new Order();
        if (StringUtils.isNotBlank(request.getParameter("userId"))) {
            order.setMemberId(Integer.parseInt(request.getParameter("userId")));
        }
        if(request.getParameter("status").equals("4")){
            order.setStatus(null);
        }else if(request.getParameter("status").equals("0")){
            order.setStatus("DISBURSING");//放款中
        }else if(request.getParameter("status").equals("1")){
            order.setStatus("REPAYMENTS");//待还款
        }else if(request.getParameter("status").equals("2")){
            order.setStatus("ONGOING");//正在还款
        }else if(request.getParameter("status").equals("3")){
            order.setStatus("COMPLETED");//已还清
        }
        startPage();
        return new XResult("查询成功", getDataTable(orderService.selectOrderList(order)));
    }



    @PostMapping("repayPlanList")
    @FilterAnnotation(validator = true, page = true)
    @ApiOperation(notes = "repayPlanList",response = OrderRepayPlanResponse.class, value = "还款计划列表")
    public Object repayPlanList(OrderRepayRequest request) {
        return new XResult("查询成功", getDataTable(orderService.repayPlanList(request)));
    }



    /**
     * 用户借款记录查询
     * @return
     */
    @PostMapping("getloanRecordList")
    @ApiOperation(notes = "getloanRecordList", value = "用户借款记录查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "当前显示数据条数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "status", value = "订单状态（0：放款中，1：待还款2：正在还款，3:已还清4：全部）", required = true, paramType = "query", dataType = "int"),
    })
    public Object getloanRecordList(@ApiIgnore HttpServletRequest request)  {
        Order order=new Order();
        if(StringUtils.isNotBlank(request.getParameter("userId"))){
            order.setMemberId(Integer.parseInt(request.getParameter("userId")));
        }
//        if(request.getParameter("status").equals("4")){
//            order.setStatus(null);
//        }else{
//        }
        if(request.getParameter("status").equals("4")){
            order.setStatus("ONGOING");//正在还款
        }else if(request.getParameter("status").equals("0")){
            order.setStatus("DISBURSING");//放款中
        }else if(request.getParameter("status").equals("1")){
            order.setStatus("REPAYMENTS");//待还款
        }else if(request.getParameter("status").equals("2")){
            order.setStatus("ONGOING");//正在还款
        }else if(request.getParameter("status").equals("3")){
            order.setStatus("COMPLETED");//已还清
        }
        startPage();
        List<Order> orderList=orderService.selectOrderListByuserId(order);
        return new XResult("查询成功", getDataTable(orderList));
    }


    /**
     * 根据用户查询还款记录
     * @param request
     * @return
     */
    @PostMapping("findPaymentHistoryListByUserId")
    @ApiOperation(notes = "findPaymentHistoryListByUserId", value = "根据用户查询还款记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "当前显示数据条数", required = false, paramType = "query", dataType = "int"),
    })
    public Object findPaymentHistoryListByUserId(@ApiIgnore HttpServletRequest request) {
        startPage();
        TableDataInfo tableDataInfo=getDataTable(paymentHistoryService.findPaymentHistoryListByUserId(Integer.valueOf(request.getParameter("userId"))));
        return new XResult("查询成功", tableDataInfo);
    }



}
